﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerInput : IUserInput
{    

    public string keyJup;
    public string keyJright;
    public string keyJdown;
    public string keyJleft;
    public string keyUp;
    public string keyRight;
    public string keyDown;
    public string keyLeft;        

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Jup = (Input.GetKey(keyJup) ? 1.0f : 0.0f) - (Input.GetKey(keyJdown) ? 1.0f : 0.0f);
        Jright = (Input.GetKey(keyJright) ? 1.0f : 0.0f) - (Input.GetKey(keyJleft) ? 1.0f : 0.0f);
       
        targetDright = (Input.GetKey(keyRight) ? 1.0f : 0.0f) - (Input.GetKey(keyLeft) ? 1.0f : 0.0f);
        targetDup = (Input.GetKey(keyUp) ? 1.0f : 0.0f) - (Input.GetKey(keyDown) ? 1.0f : 0.0f);
        Dright = Mathf.SmoothDamp(Dright, targetDright, ref drightVelocity, 0.1f);
        Dup = Mathf.SmoothDamp(Dup, targetDup, ref dupVelocity, 0.1f);

        if (enabledInput == false) {
            Dright = 0;
            Dup = 0;
        }       

        Vector2 output = SquareToCircle(Dright, Dup);
        vec = new Vector3(output.x,0,output.y);
        mag = Mathf.Sqrt(output.x * output.x + output.y * output.y);

        isRun = Input.GetKey(KeyCode.Space);
        isDefend = Input.GetKey(KeyCode.J);

        isJump = Input.GetKeyDown(KeyCode.L);
        lb = Input.GetKeyDown(KeyCode.J);
        rb = Input.GetKeyDown(KeyCode.K);
        isLockOn = Input.GetKeyDown(KeyCode.O);

        if (-0.1 < Dup && Dup < 0.1 && -0.1 < Dright && Dright < 0.1)
        {
            return;
        }
        else {
            var msg = pb.C2S_InputMsg.CreateBuilder();
            msg.SetDup(Dup);
            msg.SetDright(Dright);
            msg.SetJup(Jup);
            msg.SetJright(Jright);
            Network.Instance.Send(pb.ID.MSG_Input, msg.Build());
        }        
    }
    
}
